{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "9175fbe4",
   "metadata": {},
   "source": [
    "# Handling trajectory data files (reading & writing)\n",
    "\n",
    "<img align=\"right\" src=\"https://anitagraser.github.io/movingpandas/assets/img/movingpandas.png\">\n",
    "\n",
    "[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/anitagraser/movingpandas/master?filepath=tutorials/2-reading-data-from-files.ipynb)\n",
    "\n",
    "**<p style=\"color:#e31883\">This notebook demonstrates the current development version of MovingPandas.</p>**\n",
    "\n",
    "For tutorials using the latest release visit https://github.com/anitagraser/movingpandas-examples.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "12edc7bb",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from geopandas import GeoDataFrame, read_file\n",
    "\n",
    "import sys\n",
    "sys.path.append(\"..\")\n",
    "import movingpandas as mpd\n",
    "mpd.show_versions()\n",
    "\n",
    "import warnings\n",
    "#warnings.simplefilter(\"ignore\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "57746585",
   "metadata": {},
   "source": [
    "## Reading Geopackages"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7580e951",
   "metadata": {},
   "source": [
    "### with DatetimeIndex"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "97da2889",
   "metadata": {},
   "outputs": [],
   "source": [
    "%%time\n",
    "gdf = read_file('data/demodata_geolife.gpkg')\n",
    "gdf['t'] = pd.to_datetime(gdf['t'])\n",
    "gdf = gdf.set_index('t').tz_localize(None)\n",
    "traj_collection = mpd.TrajectoryCollection(gdf, 'trajectory_id')\n",
    "print(traj_collection)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c59b6252",
   "metadata": {},
   "source": [
    "### without DatetimeIndex"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a914c897",
   "metadata": {},
   "outputs": [],
   "source": [
    "%%time\n",
    "gdf = read_file('data/demodata_geolife.gpkg')\n",
    "traj_collection = mpd.TrajectoryCollection(gdf, 'trajectory_id', t='t')\n",
    "print(traj_collection)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "389b5195",
   "metadata": {},
   "outputs": [],
   "source": [
    "%%time\n",
    "gdf = read_file('data/demodata_ais.gpkg')\n",
    "gdf['t'] = pd.to_datetime(gdf['Timestamp'], format='%d/%m/%Y %H:%M:%S')\n",
    "gdf = gdf[gdf.SOG>0]\n",
    "traj_collection = mpd.TrajectoryCollection(gdf, 'MMSI', min_length=100, t='t')\n",
    "print(traj_collection)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8a4ed663",
   "metadata": {},
   "source": [
    "## Reading CSVs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6bb565db",
   "metadata": {},
   "outputs": [],
   "source": [
    "%%time \n",
    "df = pd.read_csv('data/demodata_geolife.csv', delimiter=';')\n",
    "traj_collection = mpd.TrajectoryCollection(df, 'trajectory_id', t='t', x='X', y='Y')\n",
    "traj_collection.hvplot(title=str(traj_collection), line_width=7.0, tiles='StamenTonerBackground', frame_width=700, frame_height=400)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "peaceful-nomination",
   "metadata": {},
   "source": [
    "## Writing\n",
    "\n",
    "### as points"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "lesbian-picking",
   "metadata": {},
   "outputs": [],
   "source": [
    "point_gdf = traj_collection.to_point_gdf()\n",
    "point_gdf.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "subtle-whole",
   "metadata": {},
   "outputs": [],
   "source": [
    "point_gdf.to_file(\"temp.gpkg\", layer='points', driver=\"GPKG\")\n",
    "read_file('temp.gpkg', layer='points').plot()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "animated-worcester",
   "metadata": {},
   "source": [
    "### as lines"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "skilled-christianity",
   "metadata": {},
   "outputs": [],
   "source": [
    "line_gdf = traj_collection.to_line_gdf()\n",
    "line_gdf.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "sharing-birthday",
   "metadata": {},
   "outputs": [],
   "source": [
    "line_gdf.to_file(\"temp.gpkg\", layer='lines', driver=\"GPKG\")\n",
    "read_file('temp.gpkg', layer='lines').plot()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "french-director",
   "metadata": {},
   "source": [
    "### as trajectories"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "wrong-bulgarian",
   "metadata": {},
   "outputs": [],
   "source": [
    "traj_gdf = traj_collection.to_traj_gdf(wkt=True)\n",
    "traj_gdf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "connected-trunk",
   "metadata": {},
   "outputs": [],
   "source": [
    "traj_gdf.to_file(\"temp.gpkg\", layer='trajectories', driver=\"GPKG\")\n",
    "read_file('temp.gpkg', layer='trajectories').plot()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "46dc3ec2",
   "metadata": {},
   "source": [
    "## Error messages while reading\n",
    "\n",
    "The following errors are expected:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8ad4c056",
   "metadata": {},
   "source": [
    "### Missing datetime info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "86f273e1",
   "metadata": {},
   "outputs": [],
   "source": [
    "gdf = read_file('data/demodata_geolife.gpkg')\n",
    "traj_collection = mpd.TrajectoryCollection(gdf, 'trajectory_id')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0d5a9867",
   "metadata": {},
   "source": [
    "### Missing geometry info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c60cf633",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_csv('data/demodata_geolife.csv', delimiter=';')\n",
    "traj_collection = mpd.TrajectoryCollection(df, 'trajectory_id', t='t')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6b343aa7",
   "metadata": {},
   "source": [
    "## Continue exploring MovingPandas\n",
    "\n",
    "1. [Getting started](1-getting-started.ipynb)\n",
    "1. [Handling trajectory data files (reading & writing)](2-reading-data-from-files.ipynb)\n",
    "1. [TrajectoryCollection aggregation (flow maps)](3-generalization-and-aggregation.ipynb)\n",
    "1. [Stop detection](4-stop-detection.ipynb)\n",
    "1. [Working with local coordinates](5-local-coordinates.ipynb)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
